/* eslint-disable camelcase */

import utils from '@/utils/index.js'
var a = require
const QQMapWX = a('./qqmap-wx-jssdk.min.js')

const qqmapsdk = new QQMapWX({
  key: utils.qqMApKey
})

export function selectMapPoint() {
  let p = new Promise((resolve, reject) => {
    wx.chooseLocation({
      success: function(res) {
        let place_info = {
          longitude: res.longitude,
          latitude: res.latitude,
          addDetail: res.address,
          city: '',
          dist: ''
        }
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude, // 纬度
            longitude: res.longitude// 经度
          },
          // 回调成功显示位置详细信息
          success: function(res1) {
            if (res1.status === 0) {
              let ad_info = res1.result.ad_info
              let format_add = res1.result.formatted_addresses
              place_info.city = ad_info.city
              place_info.dist = ad_info.district
              place_info.addDetail = format_add.recommend
            }
          },
          fail: function(error) {
            console.error(error)
          },
          complete: function(res) {
            console.log('res', res)
            resolve(place_info)
          }
        })
      },
      fail: function(err) {
        console.log(err)
        reject(err)
      },
      complete: function(opt) {
        console.log(opt)
      }
    })
  })
  return p
}

export function getLocation(lat, lon) {
  wx.openLocation({
    latitude: Number(lat),
    longitude: Number(lon),
    scale: 18,
    success: function(res) {
      console.log('调用成功', res)
    },
    fail: function(error) {
      console.error('error>>', error)
    }
  })
  // wx.getLocation({
  //   type: 'gcj02', // 返回可以用于 wx.openLocation 的经纬度
  //   success (res) {
  //     console.log('getLocation  res', res)
  //     const latitude = res.latitude
  //     const longitude = res.longitude
  //     wx.openLocation({
  //       latitude,
  //       longitude,
  //       scale: 18
  //     })
  //   }
  // })
}

export function calculateDistance(mapPointArr) {
  let fPoint
  let tPoints = []
  mapPointArr.forEach((item, index) => {
    if (index === 0) {
      fPoint = {
        latitude: item.latitude,
        longitude: item.longitude
      }
    } else {
      tPoints.push({
        latitude: item.latitude,
        longitude: item.longitude
      })
    }
  })
  console.log('tPoints', tPoints)
  console.log('fPoint', fPoint)
  let p = new Promise((resolve, reject) => {
    qqmapsdk.calculateDistance({
      mode: 'driving',
      from: fPoint,
      to: tPoints,
      success: function(res) {
        console.log('res', res)
        if (res.status === 0) {
          const points = res.result.elements
          let distances = []
          points.forEach((item, index) => {
            distances.push(item.distance)
          })
          let minDist = 0
          let distMm = 0
          let totalDist = 0
          distances.forEach((item, index) => {
            if (minDist === 0) {
              minDist = item
            } else if (minDist > item) {
              minDist = item
            }
            distMm += item
          })
          totalDist = distMm - minDist * (distances.length - 1)

          const rest = {
            points: points,
            totalDistM: totalDist

          }
          resolve(rest)
        } else {
          reject(res)
        }
      },
      fail: function(error) {
        console.error('error', error)
        reject(error)
      }
    })
  })
  return p
}

export default {
  selectMapPoint,
  calculateDistance,
  getLocation
}
